//
//  AYSPlayerViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 李晓理. All rights reserved.
//

#import "AYSPlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>

#import "AYSPlayerView.h"

@interface AYSPlayerViewController ()

@property (nonatomic, strong) MPMoviePlayerViewController *movieVC;

@end

@implementation AYSPlayerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    self.view.backgroundColor = [UIColor blackColor];
    [self createVideo];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createVideo
{
    NSURL *videoURL = [NSURL URLWithString:_videoSource];
    _movieVC = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
    [_movieVC.moviePlayer setScalingMode:MPMovieScalingModeNone];
    [_movieVC.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [self.view addSubview:_movieVC.view];
    
    _movieVC.moviePlayer.repeatMode = MPMovieRepeatModeOne;
    [_movieVC.view setFrame:CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH)];
    [self presentMoviePlayerViewControllerAnimated:_movieVC];
    [_movieVC.moviePlayer play];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void)moviePlayBackDidFinish:(NSNotification *)sender
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    [_movieVC.view removeFromSuperview];
    _movieVC = nil;
    
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
